package com.github.bean;

import lombok.Data;
import org.springframework.web.socket.WebSocketSession;

@Data
public class ServerUser {
    private String account;
    private String deviceID;
    private String token;
    private WebSocketSession serverSession;
    private WebSocketSession voiceSession;
    private WebSocketSession callSession;
    private long lastRequestStartCallToCard;
    private long lastRequestSendRoomId;
    private long lastDialRequestId;
    private int timeout = 10;
    private long lastSHeartbeatTime; // 记录最后一次收到心跳包的时间
    private long lastCHeartbeatTime; // 记录最后一次收到心跳包的时间
    private long lastVHeartbeatTime; // 记录最后一次收到心跳包的时间
    private int offlineCNum = 0; //离线发送次数
    private int offlineVNum = 0; //离线发送次数

    public ServerUser() {

    }

    public void setLastCHeartbeatTime(long lastCHeartbeatTime) {
        if (lastCHeartbeatTime > 0)
            offlineCNum = 0;
        this.lastCHeartbeatTime = lastCHeartbeatTime;
    }

    public void setLastVHeartbeatTime(long lastVHeartbeatTime) {
        if (lastVHeartbeatTime > 0)
            offlineVNum = 0;
        this.lastVHeartbeatTime = lastVHeartbeatTime;
    }

    public ServerUser(String account, String deviceID, String token, WebSocketSession session) {
        this.account = account;
        this.deviceID = deviceID;
        this.token = token;
        this.serverSession = session;
    }

    public boolean isServerOnline() {
        return serverSession != null && serverSession.isOpen();
    }

    public boolean isCardOnline() {
        return callSession != null && callSession.isOpen();
    }

    public boolean isVoiceOnline() {
        return voiceSession != null && voiceSession.isOpen();
    }

    public boolean isAllOnline() {
        return serverSession != null && serverSession.isOpen() && voiceSession != null && voiceSession.isOpen() && callSession != null && callSession.isOpen();
    }
}
